package com.ruoyi.project.cyq.task.task.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.cms.type.domain.CmsType;
import com.ruoyi.project.cyq.task.task.domain.Task;
import com.ruoyi.project.cyq.task.task.domain.TaskVo;
import com.ruoyi.project.cyq.task.task.mapper.TaskMapper;
import com.ruoyi.project.cyq.task.task.service.ITaskService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

@Service
public class TaskServiceImpl implements ITaskService {

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<TaskVo> selectTaskList(Task task) {
        return taskMapper.selectTaskList(task);
    }

    @Override
    public List<TaskVo> selectTaskList1(Task task) {
        return taskMapper.selectTaskList1(task);
    }

    @Override
    public List<TaskVo> selectTaskList2(Task task) {
        return taskMapper.selectTaskList2(task);
    }

    @Override
    public String checkTaskNameUnique(Task task) {
        Long taskId = StringUtils.isNull(task.getTaskId()) ? -1L : task.getTaskId();
        Task info = taskMapper.checkTaskNameUnique(task.getTaskTitle());
        if (StringUtils.isNotNull(info) && info.getTaskId() != taskId)
        {
            return UserConstants.CYQ_TASK_TITLE_NOT_UNIQUE;
        }
        return UserConstants.CYQ_TASK_TITLE_UNIQUE;
    }


    @Override
    public int insertTask(Task task,String taskStartTime,String taskEndTime) {
            Date startTime = this.convertDate(taskStartTime);
            Date endTime = this.convertDate(taskEndTime);
            task.setTaskStartTime(startTime);
            task.setTaskEndTime(endTime);
        return taskMapper.insertTask(task);
    }

    @Override
    public Task selectTaskById(long taskId) {
        return taskMapper.selectTaskById(taskId);
    }

    @Override
    public int updateTask(Task task, String taskStartTime, String taskEndTime) {
        Date startTime = this.convertDate(taskStartTime);
        Date endTime = this.convertDate(taskEndTime);
        task.setTaskStartTime(startTime);
        task.setTaskEndTime(endTime);
        return taskMapper.updateTask(task);
    }

    @Override
    public int deleteTaskByIds(String ids) {
        Long[] taskIds = Convert.toLongArray(ids);
        if(taskIds.length > 0){
            return taskMapper.deleteTaskByIds(taskIds);
        }
        return 0;
    }

    @Override
    public List<Task> selectAllTaskTitle() {
        return taskMapper.selectAllTaskTitle();
    }

    @Override
    public User selectUserNameByTaskTitle(String taskTitle) {
        return userMapper.selectUserById(taskMapper.selectTaskByTaskTitle(taskTitle).getUserId());
    }

    public Date convertDate(String time) {
        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;

    }

}
